#include <math.h>
#include "camera.h"
#include <extensions/gldml.h>

//global variabel
int key,keytrig;

void camera::clear()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void camera::init(int _width,int _height,int _kijkhoek,
                  float pos_x,  float pos_y,  float pos_z,
                  float view_x, float view_y, float view_z)
{
	Pos.x = pos_x;
    Pos.y = pos_y;
    Pos.z = pos_z;
    
	View.x = view_x;
    View.y = view_y;
    View.z = view_z;
	
    width = _width;
    height = _height;
     
    kijkhoek = _kijkhoek;
}

void camera::chkinput(int _width, int _height, int _marge, float _speed)
{
    //get input
    DML_input_getmousepos(&mx,&my, &mb);
    DML_input_getkeys(&key, &keytrig);
    
    //with your mouse you can move left and right, up and down.
    if( mx > (_width - _marge) && yangle > -360) 
    {
        yangle-=0.5;
    }
    
    if( mx < _marge && yangle < 360) 
    {
        yangle+=0.5;
    }
    
    //part to move te camera
    if(key == 1){  move(0.1f,0,yangle);}    //vooruit
    if(key == 2){  move(-0.1f,0,yangle);}   //achteruit
}

void camera::look()
{
    //Set projection matrix (mode opengl)
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, width, height);
    
    glPerspective(kijkhoek,1.0f,1,1000);
    
    View.x = Pos.x + sin( yangle * (PI/180) ) * 2;
    View.z = Pos.z + cos( yangle * (PI/180) ) * 2;
    
    //look
	glLookAt(Pos.x, Pos.y, Pos.z, View.x,View.y,View.z);
}

void camera::move(float _speed, int _type, int _yangle)
{
	if(Pos.x >= -35.0f && Pos.x <= 35.0f && Pos.z >= -35.0f && Pos.z <= 35.0f)
	{
	   if(_type == 0)
	   {
	      Pos.x += sin( _yangle * (PI/180) ) * _speed;
	      Pos.z += cos( _yangle * (PI/180) ) * _speed;
       }
    
       if(_type == 1)
       {
          Pos.x += sin( (_yangle + 90) * (PI/180) ) * _speed;
	      Pos.z += cos( (_yangle + 90) * (PI/180) ) * _speed;
       }
    
       if(_type == 2)
       {
	      Pos.x += sin( (_yangle - 90) * (PI/180) ) * _speed;
	      Pos.z += cos( (_yangle - 90) * (PI/180) ) * _speed;
	   }
    }
    else
    {
        if( Pos.x > 33.0f)Pos.x-=_speed;
        if( Pos.x < -33.0f)Pos.x+=_speed;
        
        if( Pos.z > 33.0f)Pos.z-=_speed;
        if( Pos.z < -33.0f)Pos.z+=_speed;
    }
}
